package top.mypath.page;

import net.miginfocom.swing.MigLayout;

import javax.swing.*;
import java.awt.*;
import java.util.EventListener;

/**
 * @author LiYalin
 */
public class ProgressBarDialog extends JDialog implements EventListener {

    private JProgressBar progressBar;

    private JButton endButton;

    private Integer completionRate;

    public ProgressBarDialog() {
    }

    public ProgressBarDialog(JFrame frame, String title) {
        super(frame, title);
        this.setSize(400, 180);
        JPanel contentPane = new JPanel(new MigLayout("wrap 4","[grow,fill,25%][grow,fill,25%][grow,25%][grow,fill,25%]","25%"));
        progressBar = new JProgressBar(0, 100);
        progressBar.setSize(400,50);
        progressBar.setForeground(Color.blue); //设置前景色为绿色
        progressBar.setBackground(Color.WHITE); //设置背景色为白色
        completionRate = 0;
        progressBar.setStringPainted(true);
        contentPane.add(progressBar,"span");
        endButton = new JButton("关闭");
        endButton.setSize(50,20);
        contentPane.add(endButton,"cell 1 1 2 1");
        endButton.setEnabled(Boolean.FALSE);
        endButton.addActionListener(e -> {
            System.exit(0);
        });
        setContentPane(contentPane);
    }

    public Integer getCompletionRate() {
        return completionRate;
    }

    public void setCompletionRate(Integer completionRate) {
        this.completionRate = completionRate;
        progressBar.setValue(completionRate);
        if (completionRate == 100) {
            endButton.setEnabled(Boolean.TRUE);
        }
    }
}



